//
//  RTNavigationBar.m
//  AutobotsPlugin
//
//  Created by 张志恒 on 16/11/7.
//  Copyright © 2016年 Autohome. All rights reserved.
//

#import "RTNavigationBar.h"
#import <objc/runtime.h>

@implementation RTNavigationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    self.backgroundColor = [UIColor whiteColor];
    
    //    [self.layer setShadowOffset:CGSizeMake(0, 1.0f/UIScreen.mainScreen.scale)];
    //    [self.layer setShadowRadius:0];
    //    [self.layer setShadowColor:[UIColor blackColor].CGColor];
    //    [self.layer setShadowOpacity:0.25f];
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.25f];
    bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:bottomLine];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomLine]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bottomLine)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottomLine(==one)]|" options:0 metrics:@{@"one": @(1.0f/UIScreen.mainScreen.scale)} views:NSDictionaryOfVariableBindings(bottomLine)]];
    
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_titleLabel];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[_titleLabel]-50-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabel)]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:10]];
}

@end

@implementation UIViewController (RTNavigationBar)

- (RTNavigationBar *)rt_navigationBar
{
    return objc_getAssociatedObject(self, @selector(rt_navigationBar));
}

- (void)setRt_navigationBar:(RTNavigationBar *)rt_navigationBar
{
    objc_setAssociatedObject(self, @selector(rt_navigationBar), rt_navigationBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)rt_isNavigationBarHidden
{
    return [objc_getAssociatedObject(self, @selector(rt_isNavigationBarHidden)) boolValue];
}

- (void)setRt_navigationBarHidden:(BOOL)rt_navigationBarHidden
{
    objc_setAssociatedObject(self, @selector(rt_isNavigationBarHidden), @(rt_navigationBarHidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self rt_setNavigationBarHidden:rt_navigationBarHidden animated:NO];
}

- (void)rt_setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated
{
    void(^block)() = ^{
        CGRect frame = self.rt_navigationBar.frame;
        frame.origin.y = hidden ? -frame.size.height : 0.0;
        self.rt_navigationBar.frame = frame;
    };
    
    if (animated) {
        [UIView animateWithDuration:0.25
                         animations:block
                         completion:^(BOOL finished) {
                             self.rt_navigationBarHidden = hidden;
                         }];
    } else {
        block();
    }
}

@end
